package com.chuanyang.driverh.mainmy;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.chuanyang.driverh.R;
import com.chuanyang.driverh.base.BaseTitleActivity;
import com.chuanyang.driverh.base.OnCustomListener;
import com.chuanyang.driverh.mainmy.adapter.BankCardAdapter;
import com.chuanyang.driverh.responseresult.BankCardData;
import com.chuanyang.driverh.responseresult.BankCardResult;
import com.chuanyang.driverh.responseresult.Result;
import com.chuanyang.driverh.utils.ActivityUtil;
import com.chuanyang.driverh.utils.ConstUtil;
import com.chuanyang.driverh.utils.DialogUtil;
import com.chuanyang.driverh.utils.PrintUtil;
import com.chuanyang.driverh.utils.SharedPrefUtil;
import com.chuanyang.driverh.utils.net.JsonGenericsSerializator;
import com.chuanyang.driverh.utils.net.MyCallback;
import com.chuanyang.driverh.utils.net.MyHttpUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import okhttp3.Call;


public class AccountBankCardActivity extends BaseTitleActivity {

    private ListView lv_bank_card;
    private List<BankCardData> bankCardEntityList = new ArrayList<>();
    private BankCardAdapter bankCardAdapter;
    private View view_foot;
    private TextView tv_add_new_bank_card;
    private HashMap params;

    @Override
    protected int getContentResId() {
        return R.layout.my_act_bankcard;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        lv_bank_card = (ListView) findViewById(R.id.lv_bank_card);
        view_foot = LayoutInflater.from(AccountBankCardActivity.this).inflate(R.layout.my_item_bank_card_foot,null);
        tv_add_new_bank_card = (TextView) view_foot.findViewById(R.id.tv_add_new_bank_card);


    }

    @Override
    protected void initData() {
        setBackImg();
        getTitleTextView().setText("银行卡");
        lv_bank_card.addFooterView(view_foot);
        bankCardAdapter = new BankCardAdapter(AccountBankCardActivity.this, bankCardEntityList);
        lv_bank_card.setAdapter(bankCardAdapter);
        bankCardAdapter.setOnCustomListener(new OnCustomListener() {
            @Override
            public void onCustomerListener(View v, int position) {
                switch (v.getId())
                {
                    case R.id.tv_delete:
                        deleteBankCard(position,bankCardEntityList.get(position));
                        break;
                }
            }
        });


    }

    @Override
    protected void initListener() {
        tv_add_new_bank_card.setOnClickListener(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        getBankCardList();
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.tv_add_new_bank_card:
                ActivityUtil.startActivity(activityContext,AddBanKCardActivity.class);
                break;
        }
    }



    private void deleteBankCard(final int position,BankCardData data) {
        DialogUtil.showLoadingDialog(activityContext,"删除...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("id", data.getId()+"");

        MyHttpUtil.deleteBankCard(activityContext,params, new MyCallback<Result>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }

            @Override
            public void onSuccess(Result result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    bankCardEntityList.remove(position);
                    bankCardAdapter.notifyDataSetChanged();
                }else{
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }
        });

    }
    private void getBankCardList() {
        DialogUtil.showLoadingDialog(activityContext,"列表...");
        Map<String,String> params=new HashMap<String, String>();
        params.put("userId", SharedPrefUtil.getUserId());

        MyHttpUtil.deleteBankCard(activityContext,params, new MyCallback<BankCardResult>(activityContext,new JsonGenericsSerializator()) {
            @Override
            public void onFail(Call call, Exception e, int id) {
                e.printStackTrace();
                PrintUtil.toast(activityContext, "网络异常，请稍后再试");
            }

            @Override
            public void onSuccess(BankCardResult result, int id) {
                if (result.getCode() == ConstUtil.OK) {
                    bankCardEntityList=result.getData();
                    bankCardAdapter.setList(bankCardEntityList);

                }else{
                    PrintUtil.toast(activityContext,result.getMsg());
                }
            }

        });
    }

}
